package com.inf.controller;

import com.inf.featureCompare.FeatureCompare;
import com.inf.utils.FeatureUtil;
import com.sun.jersey.core.util.Base64;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.web.multipart.MultipartFile;

/**
 * Created by zhangbn on 2018/11/13.
 */

@RestController
@RequestMapping("/personface/common")
public class FeatureCompareController {

    /**
     * 传入图片路取特征码
     *
     * @param pic
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/feature", method = RequestMethod.POST)
    public Map<String, String> getFeature(@RequestParam MultipartFile pic) throws IOException {
        String f = FeatureUtil.getFeatureStrByMultipartFile(pic);
        System.out.println("feature= " + f);
        Map<String, String> rstMap = new HashedMap();
        rstMap.put("feature", f);
        return rstMap;
    }


    /**
     * 传入两张图片的特征码，获得相似度
     *
     * @param feature1
     * @param feature2
     * @return
     */
    @RequestMapping(value = "/compare2feature", method = RequestMethod.POST)
    public Map<String, Double> getSimiDegree(@RequestParam String feature1, @RequestParam String feature2) {
        System.out.println("feature1= " + feature1);
        System.out.println("feature2= " + feature2);

        byte[] var1 = Base64.decode(feature1);
        byte[] var2 = Base64.decode(feature2);
        List<byte[]> list = new ArrayList();
        list.add(var2);
        double[] dArr = new FeatureCompare().featureCompByByte(var1, list);
        Map<String, Double> rstMap = new HashedMap();
        System.out.println("show: 二者相似度为 ------------------------> " + dArr[0]);
        rstMap.put("featureSimi", dArr[0]);
        return rstMap;
    }

    /**
     * 对比两张图片的相似度
     *
     * @param pic1
     * @param pic2
     * @return
     */
    @RequestMapping(value = "/compare2picture", method = RequestMethod.POST)
    public Map<String, Double> compare2pic(@RequestParam MultipartFile pic1, @RequestParam MultipartFile pic2) throws IOException {
        String f1 = FeatureUtil.getFeatureStrByMultipartFile(pic1);
        String f2 = FeatureUtil.getFeatureStrByMultipartFile(pic2);
        System.out.println("picFeature1= " + f1);
        System.out.println("picFeature2= " + f2);
        byte[] var1 = Base64.decode(f1);
        byte[] var2 = Base64.decode(f2);
        List<byte[]> list = new ArrayList();
        list.add(var2);
        double[] dArr = new FeatureCompare().featureCompByByte(var1, list);
        Map<String, Double> rstMap = new HashedMap();
        System.out.println("show: 两图相似度为 ------------------------> " + dArr[0]);
        rstMap.put("picSimi", dArr[0]);
        return rstMap;
    }

}
